package org.sippr.service.impl;

import org.sippr.mapper.ProjectMapper;
import org.sippr.pojo.Project;
import org.sippr.service.ProjectService;
import org.sippr.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ProjectImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public List<Project> list() {
        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        System.out.println(id);
        Integer userId = Integer.parseInt(id);

        return projectMapper.selectByUserId(userId);
    }

    @Override
    public Project findByName(String name) {
        return projectMapper.findByName(name);
    }

    @Override
    public void add(Project value) {
        Map<String, Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        value.setCreateUser(id);
        value.setModifyUser(id);
        LocalDateTime now = LocalDateTime.now();
        value.setCreateTime(now);
        value.setModifyTime(now);
        String uuid = UUID.randomUUID().toString();

        value.setUuid(uuid);

        projectMapper.insert(value);

    }
}
